package com.spark.modules.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.spark.common.core.Result;
import com.spark.modules.system.entity.params.LoginParams;
import com.spark.modules.system.entity.params.RegisterParams;
import com.spark.modules.system.entity.params.ResetPasswordParams;
import com.spark.modules.system.service.impl.SysLoginService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 系统登录
 *
 * @author LYCHEE
 * @date 2025/2/18 14:52
 */
@RestController
@RequestMapping(value = "/auth")
public class SysLoginController {

    @Resource
    private SysLoginService loginService;

    /**
     * 登录接口
     *
     * @param params 登录参数
     * @return token
     */
    @PostMapping(value = "/login")
    public Result<?> login(@Valid @RequestBody LoginParams params) {
        String token = loginService.login(params);
        JSONObject json = new JSONObject();
        json.put("accessToken", token);
        return Result.ok("登录成功!", json);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping(value = "/getUserInfo")
    public Result<?> getUserInfo() {
        return loginService.getUserInfo();
    }

    /**
     * 获取用户的权限编码
     *
     * @return 权限编码
     */
    @GetMapping(value = "/getAccessCodes")
    public Result<?> getAccessCodes() {
        return loginService.getAccessCodes();
    }

    /**
     * 获取用户的菜单
     *
     * @return 菜单数据
     */
    @GetMapping(value = "/getMenuList")
    public Result<?> getMenuList() {
        return loginService.getMenuList();
    }

    /**
     * 刷新token
     *
     * @return 刷新
     */
    @PostMapping(value = "/refresh")
    public Result<?> refresh(HttpServletRequest request) {
        return loginService.refresh(request);
    }

    /**
     * 退出登录
     *
     * @return 退出登录
     */
    @PostMapping(value = "/logout")
    public Result<?> logout(HttpServletRequest request) {
        return loginService.logout(request);
    }

    /**
     * 用户注册
     *
     * @param params 用户信息
     * @return 注册结果
     */
    @PostMapping(value = "/register")
    public Result<?> register(@Valid @RequestBody RegisterParams params) {
        return loginService.register(params);
    }

    /**
     * 重置密码
     *
     * @param params 密码实体
     * @return 重置结果
     */
    @PostMapping(value = "/resetPassword")
    public Result<?> resetPassword(@Valid @RequestBody ResetPasswordParams params) {
        return loginService.resetPassword(params);
    }


}
